package hdoj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class DFSPrimeCircle1016 {

	//前0 ~ 40的素数表
	private static final boolean[] prime = {false, false, true, true, false, true, false, true, false,
		false, false, true, false, true, false, false, false, true, false, true, false, false, false,
		true, false, false, false, false, false, true, false, true, false, false, false, false, false,
		true, false, false, false};
	
	private static final int MAX = 22;
	
	
	final int[] map = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
	boolean[] visited = new boolean[MAX];

	int n;
	List<List<Integer>> res = new ArrayList<List<Integer>>();
 	
	public static void main(String[] args) {
		DFSPrimeCircle1016 object = new DFSPrimeCircle1016();
		object.getRes();
	}

	public void getRes(){
		Scanner scanner = new Scanner(System.in);
		for(int t = 1; scanner.hasNext(); t++){
			n = scanner.nextInt();
			Arrays.fill(visited, false);
			visited[1] = true;
			res.clear();
			List<Integer> list = new ArrayList<>();
			list.add(1);
			dfs(0, 1, list);
			System.out.println("Case " + t + ":");
			int length = res.size();
			for(int i = 0; i < length; i++){
				List<Integer> cur = res.get(i);
				System.out.print(cur.get(0));
				for(int j = 1; j < n; j++){
					System.out.print(" " + cur.get(j));
				}
				System.out.println();
			}
			System.out.println();
		}
		scanner.close();
	}
	
	// Runtime: 3042MS
	public void dfs(int position, int number, List<Integer> list){
		if(position == n - 1){
			if(prime[number + 1]){
				res.add(new ArrayList<Integer>(list));
			}
			return;
		}
		for (int i = 1; i <= n; i++) {
			if(!visited[i] && prime[i + number]){
				list.add(i);
				visited[i] = true;
				dfs(position + 1, i, list);
				list.remove(list.size() - 1);
				visited[i] = false;
			}
		}
	}
}
